package com.example.demo1.controller;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestMapping("/user")
@RestController
public class UserController {

    @RequestMapping("/login")
    public Boolean login(String username, String password, HttpSession session){
        //校验参数的合法性'
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)){
            return false;
            //判断条件相当于username和password非空且长度不为0
        }
        //进行用户名和密码的校验
        if("admin".equals(username) && "admin".equals(password)){
            //设置session
            session.setAttribute("username","admin");
            return true;
        }
        return false;
    }

    @RequestMapping("/getUserInfo")
    public String getUserInfo(HttpServletRequest request){
        //从Session 获取登录用户
        HttpSession session = request.getSession(false);
        String username = null;
        if(session != null){
            username = (String) session.getAttribute("username");
        }
        return username;
    }
}
